package com.light.component;

import atlantafx.base.controls.Card;
import atlantafx.base.theme.Styles;
import com.light.model.GitProject;
import javafx.beans.binding.Bindings;
import javafx.geometry.Orientation;
import javafx.scene.control.*;
import javafx.scene.layout.HBox;
import javafx.scene.layout.VBox;
import javafx.scene.text.Text;
import javafx.util.Callback;
import org.kordamp.ikonli.antdesignicons.AntDesignIconsOutlined;
import org.kordamp.ikonli.javafx.FontIcon;

/**
 * 自定义行 - 提示框
 */
public class TooltipTableRow extends TableRow<GitProject> {

    private final Tooltip tooltip = new Tooltip();

    private final Text name = new Text();
    private final Text description = new Text();

    private final Text author = new Text();

    private final Label remoteAddr = new Label();
    private final Label localAddr = new Label();
    private final Label level = new Label();
    private final Text remark = new Text();
    private final HBox levelBox = new HBox();

    public static Callback<TableView<GitProject>, TableRow<GitProject>> forTableRow() {
        return param -> new TooltipTableRow();
    }

    public TooltipTableRow() {
        Card toolTipCard = new Card();
        tooltip.setGraphic(toolTipCard);
        tooltip.getStyleClass().removeAll("tooltip");
        toolTipCard.getStyleClass().add(Styles.ELEVATED_1);
        toolTipCard.setMinWidth(300);
        toolTipCard.setMaxWidth(700);
        name.getStyleClass().add(Styles.TITLE_3);
        description.wrappingWidthProperty().bind(toolTipCard.widthProperty().subtract(50));
        remark.wrappingWidthProperty().bind(Bindings.subtract(toolTipCard.widthProperty(), 50));
        VBox header = new VBox(5, name, description);
        toolTipCard.setHeader(header);

        // 分割线
        Separator separator = new Separator(Orientation.HORIZONTAL);

        VBox bodyVbox = new VBox(5);
        toolTipCard.setBody(bodyVbox);
        bodyVbox.getChildren().addAll(separator, author, remoteAddr, localAddr, levelBox, remark);
    }

    @Override
    protected void updateItem(GitProject item, boolean empty) {
        super.updateItem(item, empty);
        if (empty || item == null) {
            setTooltip(null);
        } else {
            refreshData(item);
            setTooltip(tooltip);
        }
    }

    private void refreshData(GitProject item) {
        levelBox.getChildren().clear();
        // 项目名称
        name.setText(item.name().get());
        description.setText(item.description().get());
        author.setText("仓库 作者: " + item.author().get());
        remoteAddr.setText("仓库 地址: " + item.remote());
        localAddr.setText("本地 地址: " + item.local().get());
        level.setText("学习 等级: ");
        levelBox.getChildren().add(level);
        for (int i = 0; i < item.level().get(); i++) {
            levelBox.getChildren().add(new FontIcon(AntDesignIconsOutlined.STAR));
        }
        remark.setText("仓库 备注: " + item.remark().get());
    }
}
